package com.liu.nb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.nb.entity.Debugtask;
import com.liu.nb.entity.Patroltask;
import com.liu.nb.entity.SysUser;
import com.liu.nb.mapper.PatroltaskMapper;
import com.liu.nb.service.IPatroltaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.nb.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 巡检任务表 服务实现类
 * </p>
 *
 * @author yzl
 * @since 2021-08-31
 */
@Service
public class PatroltaskServiceImpl extends ServiceImpl<PatroltaskMapper, Patroltask> implements IPatroltaskService {

    @Autowired
    private PatroltaskMapper patroltaskMapper;
    @Autowired
    private IUserService userService;

    @Override
    public int savePatrolTask(Patroltask patroltask) {
        return patroltaskMapper.insert(patroltask);
    }

    @Override
    public int updateState(Patroltask patroltask) {
        return patroltaskMapper.updateById(patroltask);
    }


    @Override
    public IPage<Patroltask> getTask(Patroltask patroltask, String name,Integer userId, Integer pageNo, Integer size) {
        Page<Patroltask> page = null;
        //判断查询条件的下发人名字是否是空，不是空就查询下发人id
        Integer id = null;
        if (patroltask.getUname() != null && !patroltask.getUname().equals("")) {
            //根据下发人姓名查询下发人id
            id = userService.getId(patroltask.getUname());
        }
        String substring = name.substring(0, 3);
        if (substring.equals("xun")) {
            //根据userId查询巡检员的所有任务并分页
                QueryWrapper<Patroltask> queryWrapper = new QueryWrapper<Patroltask>()
                        .like(ObjectUtil.isNotNull(patroltask.getId()), "id", patroltask.getId())
                        .eq(ObjectUtil.isNotNull(patroltask.getName()), "name", patroltask.getName())
                        .eq(ObjectUtil.isNotNull(patroltask.getState()), "state", patroltask.getState())
                        .eq(ObjectUtil.isNotNull(id), "admin_uid", id)
                        .ge(ObjectUtil.isNotNull(patroltask.getFirstTime()), "creartime", patroltask.getFirstTime())
                        .le(ObjectUtil.isNotNull(patroltask.getLastTime()), "creartime", patroltask.getLastTime())
                        .inSql("id", "SELECT patroltask_user.partol_id FROM patroltask_user WHERE patroltask_user.user_id = " + userId);
                page = this.page(new Page<>(pageNo, size), queryWrapper);

                page.getRecords().forEach(i -> {
                    //根据得到的巡检任务中的下发人id查询下发人的信息
                    i.setUser(userService.getById(i.getAdminUid()));
                    /**
                     * 给执行人赋值，多个
                     */
                    List<SysUser> userList = userService.list(new QueryWrapper<SysUser>()
                            .inSql("id", "SELECT user_id FROM patroltask_user WHERE partol_id ='" + i.getId() + "' "));
                    i.setZhiXing(userList);
                });
        } else if(substring.equals("adm")){
            /**
             * 线路管理员根据条件查询巡检任务wapper
             */
            QueryWrapper<Patroltask> queryWrapper =
                    new QueryWrapper<Patroltask>().like(StrUtil.isNotBlank(patroltask.getId()), "id", patroltask.getId())
                            .eq(ObjectUtil.isNotNull(patroltask.getName()), "name",patroltask.getName())
                            .eq(ObjectUtil.isNotNull(patroltask.getState()), "state", patroltask.getState())
                            .eq(ObjectUtil.isNotNull(id), "admin_uid", id)
                            .ge(ObjectUtil.isNotNull(patroltask.getFirstTime()), "creartime", patroltask.getFirstTime())
                            .le(ObjectUtil.isNotNull(patroltask.getLastTime()), "creartime", patroltask.getLastTime());

            page = this.page(new Page<>(pageNo, size), queryWrapper);
            page.getRecords().forEach(i -> {
                /*
                 * 给创建人赋值
                 * */
                i.setUser(userService.getById(i.getAdminUid()));
                /*
                 * 给执行人赋值
                 * */
                List<SysUser> userList = userService.list(new QueryWrapper<SysUser>()
                        .inSql("id", "SELECT user_id FROM patroltask_user WHERE partol_id ='" + i.getId() + "' "));
                i.setZhiXing(userList);
            });
        }
        return page;
    }




}
